/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TRCGUI.java
 *
 * Created on Aug 4, 2009, 3:43:49 AM
 */
package JavaAssignment;

import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class TRCGUI extends javax.swing.JFrame implements Serializable {

    GameData gd = new GameData();
    TransactionRecord tr;
    LinkList transactionRecordWritten = new LinkList();
    int i;

    public TRCGUI() {
        initComponents();
    }

    public void writeObject(String filename) {
        //writing in the trcollection linklist which holds transaction records
        LinkList write = (LinkList) Main.trCollection;
        FileOutputStream fos = null;
        ObjectOutputStream out = null;
        try {
            fos = new FileOutputStream(filename);
            out = new ObjectOutputStream(fos);
            out.writeObject(write);
            out.close();
            System.out.println("trCollection has been written");
        } catch (IOException ex) {
            ex.printStackTrace();

        }
    }

    public void readObject(String filename) throws FileNotFoundException, IOException, ClassNotFoundException {
        ObjectInputStream inStream = null;
        File f = new File(filename);
        inStream = new ObjectInputStream(new FileInputStream(f));
        transactionRecordWritten = (LinkList) inStream.readObject();
        System.out.println("trCollection has been read");
    }

    public void writeText() {
        textBox.setText("Record id\tGames \t\tGenre\tPrice\n");
        textBox.append("============\t============\t\t=====\t=======\n");
        for (int i = 0; i < Main.trCollection.getNoOfElement(); i++) {
            tr = (TransactionRecord) Main.trCollection.get(i);
            for (int y = 0; y < Main.shopCart.getNoOfElement(); y++) {
            }


        }


        /**
        for (int i = 0; i < Main.trCollection.getNoOfElement() - 1; i++) {
        GameData go = gc.getGame(i);
        if (go.getGN().length() > 10) {
        textBox.append(id.getSerialID(i) + "\t" + go.getGN() + "\t" + go.getGC() + "\t" + go.getGP() + "\t" + "\n");
        } else {
        textBox.append(id.getSerialID(i) + "\t" + go.getGN() + "\t\t" + go.getGC() + "" + "\t" + go.getGP() + "\t" + "\n");
        }
        }
         */
        textBox.append("\nTotal price: " + gd.getGP());

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        textBox = new javax.swing.JTextArea();
        Retrieve = new javax.swing.JButton();
        Close = new javax.swing.JButton();
        Back = new javax.swing.JButton();
        Generate = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(0, 0, 0));

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/javaassignment/sp_logo.jpg"))); // NOI18N

        textBox.setColumns(20);
        textBox.setRows(5);
        jScrollPane1.setViewportView(textBox);

        Retrieve.setText("Retrieve");
        Retrieve.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RetrieveActionPerformed(evt);
            }
        });

        Close.setText("Close");
        Close.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CloseActionPerformed(evt);
            }
        });

        Back.setText("Back");
        Back.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BackActionPerformed(evt);
            }
        });

        Generate.setText("Generate");
        Generate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                GenerateActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
                        .addGap(10, 10, 10))
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 510, javax.swing.GroupLayout.PREFERRED_SIZE)))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(48, 48, 48)
                .addComponent(Generate)
                .addGap(34, 34, 34)
                .addComponent(Retrieve)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 58, Short.MAX_VALUE)
                .addComponent(Back)
                .addGap(54, 54, 54)
                .addComponent(Close)
                .addGap(62, 62, 62))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 13, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Retrieve)
                    .addComponent(Close)
                    .addComponent(Back)
                    .addComponent(Generate))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 520, Short.MAX_VALUE)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 366, Short.MAX_VALUE)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BackActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
        new MainPageGUI().setVisible(true);
    }//GEN-LAST:event_BackActionPerformed

    private void CloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CloseActionPerformed
        // TODO add your handling code here:
        try {
            readObject("receipt.dat");
        } catch (Exception ex) {
            writeObject("receipt.dat");
            System.exit(0);
        }
        writeObject("receipt.dat");
        System.exit(0);
    }//GEN-LAST:event_CloseActionPerformed

    @SuppressWarnings("empty-statement")
    private void RetrieveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RetrieveActionPerformed
        // TODO add your handling code here:
        try {
            readObject("receipt.dat");
            System.out.println("This is the number of elements inside trc :" + transactionRecordWritten.getNoOfElement());
            textBox.setText("====================Transaction Record===================\n");
            for (int count = 0; count < transactionRecordWritten.getNoOfElement(); count++) {
                textBox.append("Record id\tGames \t\tGenre\tPrice\n");
                textBox.append("============\t============\t\t=====\t=======\n");
                tr = (TransactionRecord) transactionRecordWritten.get(count);

                for(i = 0; i < tr.getCart().getNoOfElement(); i++;){
                    ShoppingCart sc = (ShoppingCart) tr.getCart().get(i);
                    System.out.println("Iteration:"+i+"\t"+sc.gameName);
                }
            }
            /**

            for (int xky = 0; xky <  - 1; xky++) {
            gd = gc.getGame(count1);

            if (gd.getGN().length() > 10) {
            textBox.append(tr.getSerialID() + "\t" + gd.getGN() + "\t" + gd.getGC() + "\t" + gd.getGP() + "\n");
            } else {
            textBox.append(tr.getSerialID() + "\t" + gd.getGN() + "\t\t" + gd.getGC() + "\t" + gd.getGP() + "\n");
            }
            }
            textBox.append("=========================END============================\n\n\n");
            }
             ** */
        } catch (FileNotFoundException ex) {
            Logger.getLogger(TRCGUI.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(TRCGUI.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(TRCGUI.class.getName()).log(Level.SEVERE, null, ex);
        }


        //text.append(add.getSerialID(i)+"\t"+Go.getName()+"\t"+Go.getGenre()+"\t"+Go.getPrice()+"\t"+"\n");
// TODO add your handling code here:

        //text.append(add.getSerialID(i)+"\t"+Go.getName()+"\t"+Go.getGenre()+"\t"+Go.getPrice()+"\t"+"\n");

    }//GEN-LAST:event_RetrieveActionPerformed

    private void GenerateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_GenerateActionPerformed
        // TODO add your handling code here:
        writeText();
    }//GEN-LAST:event_GenerateActionPerformed
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new TRCGUI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Back;
    private javax.swing.JButton Close;
    private javax.swing.JButton Generate;
    private javax.swing.JButton Retrieve;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea textBox;
    // End of variables declaration//GEN-END:variables
}
